package com.wen.controller;

import com.github.pagehelper.PageInfo;
import com.wen.pojo.Member;
import com.wen.pojo.Orders;
import com.wen.pojo.Product;
import com.wen.pojo.Traveller;
import com.wen.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author 20212
 * 订单管理
 */
@Controller
@RequestMapping("/orders")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    @RequestMapping("/findAll")
    public ModelAndView findAll(@RequestParam(name = "page", required = true, defaultValue = "1") Integer page,
                                @RequestParam(name = "size", required = true, defaultValue = "4") Integer size) throws Exception {
        ModelAndView model = new ModelAndView();
        List<Orders> all = ordersService.findAll(page, size);
        /**
         * PageInfo就是一个分页德Bean
         */
        PageInfo pageInfo = new PageInfo(all);
        model.addObject("pageInfo", pageInfo);
        model.setViewName("order-list");
        return model;
    }

    @RequestMapping("findById")
    public ModelAndView findById(@RequestParam(name = "id", required = true) Integer ordersId) throws Exception {
        ModelAndView model = new ModelAndView();
        Orders orders = ordersService.findById(ordersId);
        model.addObject("orders", orders);
        model.setViewName("order-show");
        return model;
    }

    @RequestMapping("/deleteById")
    public String deleteById(@RequestParam(name = "id", required = true) Integer oid) throws Exception {
        ordersService.deleteById(oid);
        return "redirect:/orders/findAll";
    }

    @RequestMapping("/toAdd")
    public ModelAndView toAdd() throws Exception {
        ModelAndView model = new ModelAndView();
        List<Member> memberList = ordersService.findAllMember();
        List<Product> productList = ordersService.findAllProduct();
        model.addObject("memberList", memberList);
        model.addObject("productList", productList);
        model.setViewName("order-add");
        return model;

    }

    @RequestMapping("/add")
    public ModelAndView add(Orders orders) throws Exception {
        ModelAndView model = new ModelAndView();
        ordersService.add(orders);
        model.addObject("peopleCount", orders.getPeopleCount());
        model.addObject("orderNum", orders.getOrderNum());
        model.setViewName("traveller-add");
        return model;
    }

    @RequestMapping("/addTraveller")
    public ModelAndView addTraveller( Integer peopleCount, Traveller traveller, String orderNum) throws Exception {
        ModelAndView model = new ModelAndView();
        System.out.println("=>" + peopleCount);
        model.addObject("peopleCount", peopleCount - 1);
        ordersService.addTraveller(traveller);
        Integer orderId = ordersService.findIdByOrderNum(orderNum);
        Integer travellerId = traveller.getId();
        ordersService.addOrderTraveller(orderId, travellerId);
        System.out.println("=>" + peopleCount);
        if (peopleCount == 1) {
           model.setViewName("redirect:/orders/findAll");;
        } else {
         model.setViewName("traveller-add");
        }
        return model;
    }

    @RequestMapping("/editById")
    public ModelAndView editById(@RequestParam(name = "id", required = true) Integer oid) throws Exception {
        ModelAndView mv = new ModelAndView();
        Orders order = ordersService.findById(oid);
        List<Member> memberList = ordersService.findAllMember();
        List<Product> productList = ordersService.findAllProduct();
        Integer productId = order.getProductId();
        Integer memberId = order.getMemberId();
        for (Member member : memberList) {
            member.setTempId(memberId);
        }
        for (Product product : productList) {
            product.setTempId(productId);
        }
        mv.addObject("memberList", memberList);
        mv.addObject("productList", productList);
        mv.addObject("order", order);
        mv.setViewName("order-edit");
        return mv;
    }

    @RequestMapping("/edit")
    public String edit(Orders order) {
        ordersService.edit(order);
        return "redirect:/orders/findAll";
    }
}
